package org.gap.service;

import java.util.List;
import java.util.Set;

import org.gap.dao.EtablissementsBancairesDAO;
import org.gap.dao.GuichetsDAO;

import org.gap.domain.EtablissementsBancaires;
import org.gap.domain.Guichets;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Guichets entities
 * 
 */

@Service("GuichetsService")
@Transactional
public class GuichetsServiceImpl implements GuichetsService {

	/**
	 * DAO injected by Spring that manages EtablissementsBancaires entities
	 * 
	 */
	@Autowired
	private EtablissementsBancairesDAO etablissementsBancairesDAO;

	/**
	 * DAO injected by Spring that manages Guichets entities
	 * 
	 */
	@Autowired
	private GuichetsDAO guichetsDAO;

	/**
	 * Instantiates a new GuichetsServiceImpl.
	 *
	 */
	public GuichetsServiceImpl() {
	}

	/**
	 * Delete an existing EtablissementsBancaires entity
	 * 
	 */
	@Transactional
	public Guichets deleteGuichetsEtablissementsBancaires(Integer guichets_guiEtablissement, Integer guichets_guiGuichet, String guichets_guiPays, String related_etablissementsbancaires_etbPays, Integer related_etablissementsbancaires_etbEtablissement) {
		Guichets guichets = guichetsDAO.findGuichetsByPrimaryKey(guichets_guiEtablissement, guichets_guiGuichet, guichets_guiPays, -1, -1);
		EtablissementsBancaires related_etablissementsbancaires = etablissementsBancairesDAO.findEtablissementsBancairesByPrimaryKey(related_etablissementsbancaires_etbPays, related_etablissementsbancaires_etbEtablissement, -1, -1);

		guichets.setEtablissementsBancaires(null);
		related_etablissementsbancaires.getGuichetses().remove(guichets);
		guichets = guichetsDAO.store(guichets);
		guichetsDAO.flush();

		related_etablissementsbancaires = etablissementsBancairesDAO.store(related_etablissementsbancaires);
		etablissementsBancairesDAO.flush();

		etablissementsBancairesDAO.remove(related_etablissementsbancaires);
		etablissementsBancairesDAO.flush();

		return guichets;
	}

	/**
	 * Save an existing Guichets entity
	 * 
	 */
	@Transactional
	public void saveGuichets(Guichets guichets) {
		Guichets existingGuichets = guichetsDAO.findGuichetsByPrimaryKey(guichets.getGuiEtablissement(), guichets.getGuiGuichet(), guichets.getGuiPays());

		if (existingGuichets != null) {
			if (existingGuichets != guichets) {
				existingGuichets.setGuiEtablissement(guichets.getGuiEtablissement());
				existingGuichets.setGuiGuichet(guichets.getGuiGuichet());
				existingGuichets.setGuiPays(guichets.getGuiPays());
				existingGuichets.setGuiDomiciliation(guichets.getGuiDomiciliation());
				existingGuichets.setGuiAdressePrelevement(guichets.getGuiAdressePrelevement());
				existingGuichets.setGuiContactPrelevement(guichets.getGuiContactPrelevement());
				existingGuichets.setGuiChronoTrace(guichets.getGuiChronoTrace());
				existingGuichets.setGuiNomGuichet(guichets.getGuiNomGuichet());
				existingGuichets.setGuiCodeNouvEtablissement(guichets.getGuiCodeNouvEtablissement());
				existingGuichets.setGuiNouvGuichet(guichets.getGuiNouvGuichet());
				existingGuichets.setGuiLocaliteCompensation1(guichets.getGuiLocaliteCompensation1());
				existingGuichets.setGuiLocaliteCompensation2(guichets.getGuiLocaliteCompensation2());
				existingGuichets.setGuiDebutValidite(guichets.getGuiDebutValidite());
				existingGuichets.setGuiFinValidite(guichets.getGuiFinValidite());
				existingGuichets.setGuiDisparition(guichets.getGuiDisparition());
				existingGuichets.setGuiNatureGuichet(guichets.getGuiNatureGuichet());
				existingGuichets.setGuiCodeSituation(guichets.getGuiCodeSituation());
				existingGuichets.setGuiTiers(guichets.getGuiTiers());
			}
			guichets = guichetsDAO.store(existingGuichets);
		} else {
			guichets = guichetsDAO.store(guichets);
		}
		guichetsDAO.flush();
	}

	/**
	 * Return all Guichets entity
	 * 
	 */
	@Transactional
	public List<Guichets> findAllGuichetss(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Guichets>(guichetsDAO.findAllGuichetss(startResult, maxRows));
	}

	/**
	 * Load an existing Guichets entity
	 * 
	 */
	@Transactional
	public Set<Guichets> loadGuichetss() {
		return guichetsDAO.findAllGuichetss();
	}

	/**
	 */
	@Transactional
	public Guichets findGuichetsByPrimaryKey(Integer guiEtablissement, Integer guiGuichet, String guiPays) {
		return guichetsDAO.findGuichetsByPrimaryKey(guiEtablissement, guiGuichet, guiPays);
	}

	/**
	 * Delete an existing Guichets entity
	 * 
	 */
	@Transactional
	public void deleteGuichets(Guichets guichets) {
		guichetsDAO.remove(guichets);
		guichetsDAO.flush();
	}

	/**
	 * Return a count of all Guichets entity
	 * 
	 */
	@Transactional
	public Integer countGuichetss() {
		return ((Long) guichetsDAO.createQuerySingleResult("select count(*) from Guichets o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing EtablissementsBancaires entity
	 * 
	 */
	@Transactional
	public Guichets saveGuichetsEtablissementsBancaires(Integer guiEtablissement, Integer guiGuichet, String guiPays, EtablissementsBancaires related_etablissementsbancaires) {
		Guichets guichets = guichetsDAO.findGuichetsByPrimaryKey(guiEtablissement, guiGuichet, guiPays, -1, -1);
		EtablissementsBancaires existingetablissementsBancaires = etablissementsBancairesDAO.findEtablissementsBancairesByPrimaryKey(related_etablissementsbancaires.getEtbPays(), related_etablissementsbancaires.getEtbEtablissement());

		// copy into the existing record to preserve existing relationships
		if (existingetablissementsBancaires != null) {
			existingetablissementsBancaires.setEtbPays(related_etablissementsbancaires.getEtbPays());
			existingetablissementsBancaires.setEtbEtablissement(related_etablissementsbancaires.getEtbEtablissement());
			existingetablissementsBancaires.setEtbDescription(related_etablissementsbancaires.getEtbDescription());
			existingetablissementsBancaires.setEtbCodeSituation(related_etablissementsbancaires.getEtbCodeSituation());
			existingetablissementsBancaires.setEtbDateDebutValidite(related_etablissementsbancaires.getEtbDateDebutValidite());
			existingetablissementsBancaires.setEtbDateFinValidite(related_etablissementsbancaires.getEtbDateFinValidite());
			existingetablissementsBancaires.setEtbDateDisparition(related_etablissementsbancaires.getEtbDateDisparition());
			existingetablissementsBancaires.setEtbCodeEtablissNouveau(related_etablissementsbancaires.getEtbCodeEtablissNouveau());
			existingetablissementsBancaires.setEtbOptionIdInvariant(related_etablissementsbancaires.getEtbOptionIdInvariant());
			existingetablissementsBancaires.setEtbDateEffetIdInvariant(related_etablissementsbancaires.getEtbDateEffetIdInvariant());
			related_etablissementsbancaires = existingetablissementsBancaires;
		}

		guichets.setEtablissementsBancaires(related_etablissementsbancaires);
		related_etablissementsbancaires.getGuichetses().add(guichets);
		guichets = guichetsDAO.store(guichets);
		guichetsDAO.flush();

		related_etablissementsbancaires = etablissementsBancairesDAO.store(related_etablissementsbancaires);
		etablissementsBancairesDAO.flush();

		return guichets;
	}
}
